<?php

namespace App\Console\Commands;

use App\Http\Models\Mysql\SsyStyleAttachTable;
use App\Http\Models\Mysql\StyleTable;
use App\Http\Repository\CommonRepository;
use App\Http\Repository\SzdptechRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;

class downloadAttach extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:downloadAttach {--typeNo=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '下载数加设计档案附件';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $autoKey = "auto_downloadAttach";
        Cache::put($autoKey, 1, Carbon::now()->addMinutes(15));
        $typeNo = $this->option('typeNo') ?: "";
        if(!empty($typeNo)){
            $typeNo = explode(",", $typeNo);
        }else{
            $list = (new StyleTable())->where('approval_state', 0)->orderBy('creation_time', 'desc')->get(['number'])->toArray();
            $typeNo = array_column($list, 'number');
        }
        $total = count($typeNo);
        $pageTotal = ceil($total/100);
        for($i = 1; $i <= $total; $i++){
            if(empty($typeNo)){
                break;
            }
            $numberList = array_splice($typeNo, 0, 100);
            $res = SzdptechRepository::searchStyleAttach($numberList);
            $list = $res['value'] ?? [];
            if(empty($list)){
                break;
            }
            $this->setData($list, $i, $pageTotal);
        }
        Cache::forget($autoKey);
    }

    public function setData($list, $page, $pageTotal)
    {
        foreach ($list as $item) {
            $typeNo = $item['number'];
            foreach ($item['attach_list'] as $attach){
                $exist = (new SsyStyleAttachTable())->where('type_no', $typeNo)->where('name', $attach['name'])->first();
                if(!empty($exist)){
                    echo "[$page, $pageTotal]文件已存在： {$typeNo} {$attach['name']}\n";
                    continue;
                }
                $fileNameArr = explode(".", $attach['name']);
                $fileName = date('YmdHis')."_".md5($fileNameArr[0]).".".$fileNameArr[1];
                $fileRes = CommonRepository::uploadCDN($attach['url'], 'file', $fileName);
                if($fileRes['code'] != 0){
                    echo "[$page, $pageTotal]文件上传失败： {$typeNo} {$attach['url']}\n";
                    continue;
                }
                $data = [
                    'type_no' => $typeNo,
                    'name' => $attach['name'],
                    'url' => $fileRes['file'],
                    'createtime' => date('Y-m-d H:i:s')
                ];
                (new SsyStyleAttachTable())->createData($data);
                echo "[$page, $pageTotal]文件上传成功： {$typeNo} {$attach['url']}\n";
            }
        }
    }
}
